package zq.cn.activity;

import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.common.api.GoogleApiClient;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import zq.cn.utils.Globals;
import zq.cn.utils.SqlDao;

public class InitActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_init);

        Globals.init(this);

        Thread th = new Thread() {
            @Override
            public void run() {
                try {

                    SharedPreferences shared = getSharedPreferences("shared",MAX_PRIORITY);
                    boolean flag = shared.getBoolean("flag",false);
                    if(flag){
                        try {
                            sleep(2000);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }else {
                        SqlDao.deleteData();
                        InputStream is = getAssets().open("question.txt");
                        show(is);
                        SharedPreferences.Editor editor = shared.edit();
                        editor.putBoolean("flag", true);
                        editor.commit();
                    }
                }catch (IOException e) {
                    e.printStackTrace();
                }
                Intent in = new Intent(InitActivity.this, MainActivity.class);
                startActivity(in);
                finish();
            }
        };
        th.start();
    }

    private void show(InputStream is) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        BufferedReader bf = new BufferedReader(new InputStreamReader(is,"gbk"));
        String line = null;
        StringBuffer ques = new StringBuffer();
        StringBuffer anw = new StringBuffer();
        boolean flag = false;
        while((line=bf.readLine())!=null) {
            if(line.equals("Start_Question_Flag")) {
                //问题开始
                flag = true;

            }else if(line.equals("Start_Answer_Flag")){
                //答案开始
                flag = false;
            }else if(line.equals("End_Flag")) {
                //问题、答案结束，整合
                Map<String, Object> map = new HashMap<>();
                map.put("question", ques.toString());
                map.put("answer",anw.toString());
                SqlDao.insertData(map);
                //清空问题和答案
                ques = new StringBuffer();
                anw = new StringBuffer();
            }else {
                //内容读取
                if(flag) {
                    ques.append(line);
                }else {
                    anw.append(line);
                }
            }
        }
    }

}
